function refresh(item){
	item.src = "codeImg?" + Math.random();
}

$(function(){
	$("#login-container").css("height",$(document.body).height()+"px");
	$(":text").focus(function(){
	    $(this).css("border-color","#cccccc");
	});
	$("[name='code']").keypress(function(event){
		if (event.keyCode == 13) { 
			login();
        }  
	});
	$("[name='code']").blur(function(){
		var value = $(this).val();
		if(value == null || value == ""){
			$(this).css("border-color","#FF4500");
			$(".login-box-warning").text("验证码不能为空");
		}else{
			$(".login-box-warning").text("");
		}
	});
	$(":password").focus(function(){
	    $(this).css("border-color","#cccccc");
	});
	$(":password").blur(function(){
		var value = $(this).val();
		if(value == null || value == ""){
			$(this).css("border-color","#FF4500");
			$(".login-box-warning").text("密码不能为空");
		}else{
			$(".login-box-warning").text("");
		}
	});
	$("[name='account']").blur(function(){
		var name = $("[name='account']").val();
		if(name == null || name == ""){
			$(this).css("border-color","#FF4500");
			$(".login-box-warning").text("用户名不能为空");
			return;
		}
		$.ajax(
			{
				url:"main/checkName.do?account="+name,
				type:"get",
				//dataType:"json",
				//data:{userName:name},
				success:function(result){
					$(".login-box-warning").text(result);
				},
				error:function(error){
                    $(".login-box-warning").text(error);
				}
			}	
		);
	});
});


function login(){
	var account = $("[name='account']").val();
	var pass = $("[name='password']").val();
	var code = $("[name='code']").val();
	if(account == null || account == ""){
		 $(".login-box-warning").text("用户名不能为空");
		 $("[name='account']").css("border-color","#FF4500");
		 return;
	}
	if(pass == null || pass == ""){
		 $(".login-box-warning").text("密码不能为空");
		 $("[name='password']").css("border-color","#FF4500");
		 return;
	}
	if(code == null || code == ""){
		 $(".login-box-warning").text("验证码不能为空");
		 $("[name='code']").css("border-color","#FF4500");
		 return;
	}
	$.ajax(
		{
//			url:"main/login.do?userName="+name+"&password="+pass+"&code="+code,
			url:"main/login.do",
			type:"post",
			data:{
				account:account,
				password:pass,
				code:code
			},
			success:function(result){
				var i = result.indexOf("forword:");
				if (i != -1) {//根据返回值进行跳转
					var url = result.substring("forword:".length);
					window.location.href = "/TMS"+url;
					return;
			    }
				$("[name='password']").val("");
				$("[name='code']").val("");
				$(".codeImg").attr("src","codeImg?" + Math.random());
				$(".login-box-warning").text(result);
				
			},
			error:function(error){
                $(".login-box-warning").text("error:"+error);
			}
		}	
	);
	
}
